﻿<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- disable indentation, omit xml header, preserve CDATA -->
  <xsl:output method="xml" indent="no" omit-xml-declaration="yes" encoding="utf-8" cdata-section-elements="xsl:text" />

  <!-- suppress empty nodes -->
  <xsl:strip-space elements="*" />
  <xsl:preserve-space elements="xsl:text" />

  <!-- copy !DOCTYPE from comment -->
  <!-- http://www.dpawson.co.uk/xsl/sect2/N2281.html#d3801e55 -->
  <xsl:template match="/">
    <xsl:variable name="doctype" select="//comment()[contains(.,'DOCTYPE')][1]" />
    <xsl:if test="$doctype">
      <xsl:text disable-output-escaping="yes">&lt;!</xsl:text>
      <xsl:value-of disable-output-escaping="yes" select="$doctype" />
      <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    </xsl:if>
    
    <xsl:apply-templates select="* | processing-instruction() | comment()" />
  </xsl:template>
  
  <!-- copy all -->  
  <xsl:template match="@* | *">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <!-- suppress #comment nodes -->
  <xsl:template match="comment()" />

</xsl:stylesheet>
